package repo

import (
	"storeManageApi/models/dbModels"
	"storeManageApi/models/dbModels/reflectDbModels"
	"xorm.io/builder"
)

// IStoreNotesRepo
//
//	@Description:库存表的操作接口
type IStoreNotesRepo interface {
	GetPage(start, pageSize int) ([]dbModels.StoreNotes, error)
	AddRow(store dbModels.StoreNotes) (int64, error)
	GetStoreByGoodsUid(uid string) (dbModels.StoreNotes, error)
	GetCount() (int64, error)
}

// CreateStoreNotesRepo
//
//	@Description: 创建库存表的repo
//	@return: IStoreNotesRepo
func CreateStoreNotesRepo() IStoreNotesRepo {
	var storeNotesTable reflectDbModels.StoreNotesTable = reflectDbModels.GetStoreNotesTable()
	return &StoreNotesRepo{
		StoreNotesTable: storeNotesTable,
	}
}

// StoreNotesRepo
//
//	@Description:
type StoreNotesRepo struct {
	StoreNotesTable reflectDbModels.StoreNotesTable
}

func (repo StoreNotesRepo) GetCount() (int64, error) {
	return Engin.Table(repo.StoreNotesTable.Table).Count()
}

// GetStoreByGoodsUid
//
//	@Description: 根据物品uid获取一条仓库记录
//	@receiver: repo
//	@param: uid
//	@return: dbModels.StoreNotes
//	@return: error
func (repo StoreNotesRepo) GetStoreByGoodsUid(uid string) (dbModels.StoreNotes, error) {
	var result dbModels.StoreNotes = dbModels.StoreNotes{}
	_, err := Engin.Where(builder.Eq{
		repo.StoreNotesTable.GoodsUid: uid,
	}).Get(&result)
	return result, err
}

// AddRow
//
//	@Description: 添加一条库存记录
//	@receiver: repo
//	@param: store
//	@return: int64
//	@return: error
func (repo StoreNotesRepo) AddRow(store dbModels.StoreNotes) (int64, error) {
	return Engin.InsertOne(store)
}

// GetPage
//
//	@Description: 分页获取库存列表
//	@receiver: repo
//	@param: start
//	@param: pageSize
//	@return: []dbModels.StoreNotes
//	@return: error
func (repo StoreNotesRepo) GetPage(start, pageSize int) ([]dbModels.StoreNotes, error) {
	var results []dbModels.StoreNotes = make([]dbModels.StoreNotes, 0)
	err := Engin.Table(repo.StoreNotesTable.Table).Desc(repo.StoreNotesTable.Base.Id).Limit(pageSize, start-1).Find(&results)
	return results, err
}
